package com.lihaogn.sell.service.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lihaogn.sell.entity.FoodCookCategory;
import com.lihaogn.sell.entity.FoodMaterial;
import com.lihaogn.sell.entity.FoodTypeCategory;
import com.lihaogn.sell.mapper.ProductCategoryMapper;
import com.lihaogn.sell.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @FileName: ProductCategoryServiceImpl
 * @Description: TODO
 * @Author: lihao
 * @Date: 2019-05-16 16:50
 * @Version: 1.0
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public List<FoodMaterial> getAllFoodMaterial() {
        return productCategoryMapper.queryAllFoodMaterial();
    }

    @Override
    public FoodMaterial queryFoodMaterialById(int foodMaterialTempId) {
        return productCategoryMapper.queryFoodMaterialById(foodMaterialTempId);
    }

    @Override
    public FoodTypeCategory queryFoodTypeCategoryById(String categoryId) {
        return productCategoryMapper.queryFoodTypeCategoryById(categoryId);
    }

    @Override
    public FoodCookCategory queryFoodCookCategoryById(String categoryId) {
        return productCategoryMapper.queryFoodCookCategoryById(categoryId);
    }

    @Override
    public List<FoodCookCategory> getAllFoodCookCategory() {
        return productCategoryMapper.queryAllFoodCookCategory();
    }

    @Override
    public List<FoodTypeCategory> getAllFoodTypeCategory() {
        return productCategoryMapper.queryAllFoodTypeCategory();
    }

    @Override
    public int queryExistCookName(String cookName) {
        return productCategoryMapper.queryExistCookName(cookName);
    }

    @Override
    public int queryExistTypeName(String typeName) {
        return productCategoryMapper.queryExistTypeName(typeName);
    }

    @Override
    public Integer queryExistMaterialName(String name) {
        return productCategoryMapper.queryExistMaterialName(name);
    }

    @Override
    public int addOneFoodCookCategory(FoodCookCategory foodCookCategory) {
        return productCategoryMapper.insertOneFoodCookCategory(foodCookCategory);
    }

    @Override
    public int addOneFoodTypeCategory(FoodTypeCategory foodTypeCategory) {
        return productCategoryMapper.insertOneFoodTypeCategory(foodTypeCategory);
    }

    @Override
    public int addFoodMaterial(FoodMaterial foodMaterial) {
        return productCategoryMapper.insertFoodMaterial(foodMaterial);
    }

    @Override
    public int updateOneFoodCookCategory(FoodCookCategory foodCookCategory) {
        return productCategoryMapper.updateOneFoodCookCategory(foodCookCategory);
    }

    @Override
    public int updateOneFoodTypeCategory(FoodTypeCategory foodTypeCategory) {
        return productCategoryMapper.updateOneFoodTypeCategory(foodTypeCategory);
    }

    @Override
    public int updateFoodMaterial(FoodMaterial foodMaterial) {
        return productCategoryMapper.updateFoodMaterial(foodMaterial);
    }

    @Override
    public int deleteOneFoodCookCategory(String foodCookCategoryId) {
        return productCategoryMapper.deleteOneFoodCookCategory(foodCookCategoryId);
    }

    @Override
    public int deleteOneFoodTypeCategory(String foodTypeCategoryId) {
        return productCategoryMapper.deleteOneFoodTypeCategory(foodTypeCategoryId);
    }

    @Override
    public int deleteOneFoodMaterial(int materialId) {
        return productCategoryMapper.deleteOneFoodMaterial(materialId);
    }

    @Override
    public int deleteMultiFoodCookCategory(HttpServletRequest request) {

        // 记录删除的个数
        int num;
        BufferedReader reader;

        try {
            reader = request.getReader();

            StringBuffer res = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                res.append(line);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            ArrayList<String> idList = objectMapper.readValue(res.toString(), ArrayList.class);

            HashMap<String, Object> map = new HashMap<>();
            map.put("idList", idList);

            num = productCategoryMapper.deleteMultiFoodCookCategory(map);
            if (num == idList.size()) {
                System.out.println("num:"+num+","+"idList.size:"+idList.size());
                return 1;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public int deleteMultiFoodTypeCategory(HttpServletRequest request) {
        // 记录删除的个数
        int num;
        BufferedReader reader;

        try {
            reader = request.getReader();

            StringBuffer res = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                res.append(line);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            ArrayList<String> idList = objectMapper.readValue(res.toString(), ArrayList.class);

            HashMap<String, Object> map = new HashMap<>();
            map.put("idList", idList);

            num = productCategoryMapper.deleteMultiFoodTypeCategory(map);
            if (num == idList.size()) {
                System.out.println("num:"+num+","+"idList.size:"+idList.size());
                return 1;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public int deleteMultiFoodMaterial(HttpServletRequest request) {
        // 记录删除的个数
        int num;
        BufferedReader reader;

        try {
            reader = request.getReader();

            StringBuffer res = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                res.append(line);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            ArrayList<String> idList = objectMapper.readValue(res.toString(), ArrayList.class);

            HashMap<String, Object> map = new HashMap<>();
            map.put("idList", idList);

            num = productCategoryMapper.deleteMultiFoodMaterial(map);
            if (num == idList.size()) {
                System.out.println("num:"+num+","+"idList.size:"+idList.size());
                return 1;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return 0;
        }

        return 0;
    }


}
